@echo off
setlocal enabledelayedexpansion

set lib_path=%~dp0\lib\
set framework_prefix=ablecloud-framework
set framework=

set conf_file=""
set mode=test
set user_lib_path=""
set uds_id=""
set mx="1024"
set ms="256"

set framework=

if "%1" == "/?" (
    @echo Usage %0 [-c conf_file] [-l user_lib_path] [-m mode] [-i identity] [-x maximum_memory_in_MB] [-s minimum_memory_in_MB]
    @echo   -c: specify user-defined configuration file.
    @echo   -l: specify library path which you will used excluding framework and framework's lib.
    @echo   -m: specify run mode. should be one of [test,production], default is production.
    @echo   -i: identity of the UDS: domainId_subDomainId_serviceName_serviceVersion.
    @echo   -x: maximum memory in MB which can be used by the JVM.
    @echo   -s: minimum memory in MB which shoud be allocated for the JVM.
    goto :eof
)

if "%1" == "-c" (
    set conf_file=%~f2
    shift
    shift
)

if "%1" == "-l" (
    set user_lib_path=%~f2
    shift
    shift
)

if "%1" == "-m" (
    set mode=%2
    shift
    shift
)

if "%1" == "-i" (
    set uds_id=%2
    shift
    shift
)

if "%1" == "-x" (
    set mx=%2
    shift
    shift
)

if "%1" == "-s" (
    set ms=%2
    shift
    shift
)

set framework_num=0
for %%i in (%lib_path%%framework_prefix%*.jar) do (
    set framework=%%i
    set /a framework_num+=1
)

if %framework_num% gtr 1 (
    @echo more than one framework jar found
    exit /b 1
)
if %framework_num lss 1 (
    @echo missing framework jar
    exit /b 1
)

set param=-Xbootclasspath/a:
for %%i in (%lib_path%*.jar) do (
    ::echo %%i
    set param=!param!%%i;
)

if not %user_lib_path% == "" (
    if not %user_lib_path% == %lib_path% (
        for %%i in (%user_lib_path%\*.jar) do (
            set param=!param!%%i;
        )
    )
)

set args=-Dmode=%mode%
if not %conf_file% == "" (
    set args=!args! -Dconf_file=%conf_file%
)
if not %uds_id% == "" (
    set args=!args! -uds_id=%uds_id%
)

java %param% %args% -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 -Xmx%mx%m -Xms%ms%m -jar %framework%
